#include <iostream>
#include <set>
#include <vector>
#include <utility>

using std::cout;
using std::endl;
using std::set;
using std::vector;
using std::pair;

template <typename  Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    //set的基本特征
    //1、存放的是key类型，key值是唯一的，不能重复
    //2、默认情况下，会按照key值进行升序排列
    //3、底层实现使用的是红黑树
    set<int> number = {1, 3, 5, 7, 9, 8, 3, 3, 4, 3, 2};
    /* set<int, std::greater<int>> number = {1, 3, 5, 7, 9, 8, 3, 3, 4, 3, 2}; */
    display(number);

    cout << endl << "set的查找" << endl;
    size_t cnt = number.count(7);
    cout << "cnt = " << cnt << endl;
    set<int>::iterator it = number.find(8);
    if(it !=  number.end())
    {
        cout << "元素是存在 " << *it << endl;
    }
    else
    {
        cout << "元素是不存在的" << endl;
    }

    cout << endl << "set的insert操作" << endl;
    pair<set<int>::iterator, bool> ret = number.insert(100);
    if(ret.second)
    {
        cout << "插入成功 " << *ret.first<< endl;
    }
    else
    {
        cout << "插入失败 " << endl;
    }
    display(number);

    cout << endl;
    vector<int> vec = {1, 3, 5, 7, 9, 12, 34, 23};
    number.insert(vec.begin(), vec.end());
    display(number);

    cout << endl;
    number.insert({6, 34, 15, 22});
    display(number);

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.erase(it);
    display(number);

    cout << endl << endl;
    /* cout << "number[1] = " << number[1] << endl;//error */

    it = number.begin();
    /* *it = 300; */
    cout << "*it = " << *it << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

